package study.jy.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import study.jy.common.Constants;
import study.jy.common.enums.ResultCodeEnum;
import study.jy.common.enums.RoleEnum;
import study.jy.entity.Account;
import study.jy.entity.Admin;
import study.jy.exception.CustomException;
import study.jy.mapper.AdminMapper;
import study.jy.service.AdminService;
import study.jy.utils.JWTUtil;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;


    // 添加管理员
    @Override
    public void add(Admin admin) {
        // 1.判断管理员是否存在
        Admin dbAdmin = adminMapper.selectByUsername(admin.getUsername());
        if (dbAdmin != null) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }

        // 2.设置密码默认值
        if (ObjectUtil.isEmpty(admin.getPassword())) {
            admin.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }

        // 3.设置昵称为用户名
        if (ObjectUtil.isEmpty(admin.getName())) {
            admin.setName(admin.getUsername());
        }

        // 4.设置角色为管理员
        admin.setRole(RoleEnum.ADMIN.name());

        // 5.插入数据库
        adminMapper.insert(admin);
    }

    // 根据Id查询管理员
    @Override
    public Admin selectById(Integer id) {
        Admin admin = adminMapper.selectById(id);
        return admin;
    }

    // 查询所有管理员
    @Override
    public List<Admin> selectAll(Admin admin) {
        List<Admin> list = adminMapper.selectAll(admin);
        return list;
    }

    // 修改密码
    @Override
    public void updatePassword(Account account) {
        Admin dbAdmin = adminMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbAdmin)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!dbAdmin.getPassword().equals(account.getPassword())) {
            throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
        }
        dbAdmin.setPassword(account.getNewPassword());
        adminMapper.updateById(dbAdmin);
    }


    // 分页查询管理员
    @Override
    public PageInfo<Admin> selectPage(Integer pageNum, Integer pageSize, Admin admin) {
        // 1. 设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        // 2. 查询管理员
        List<Admin> adminList =adminMapper.selectAll(admin);

        // 3. 返回分页结果
        return PageInfo.of(adminList);
    }

    // 修改管理员信息
    @Override
    public void updateById(Admin admin) {
        adminMapper.updateById(admin);
    }

    // 根据Id删除管理员
    @Override
    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

    // 根据Id批量删除管理员
    @Override
    public void deleteByIds(List<Integer> ids) {
        adminMapper.deleteByIds(ids);
    }

    /**
     * 登录
     */
    public Admin login(Account account) {
        Admin dbAdmin = adminMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbAdmin)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!dbAdmin.getPassword().equals(account.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        // token
        String token = JWTUtil.createToken(dbAdmin.getId() + "-" + dbAdmin.getRole(), dbAdmin.getPassword());
        dbAdmin.setToken(token);
        return dbAdmin;
    }
}
